#include "drawgraph.h"
#include "ui_drawgraph.h"
#include <QApplication>
#include <QtWidgets>
#include <QXmlStreamWriter>
#include <QtWidgets>
#include <dbutils.h>
#include <QSqlQuery>
#include <QChart>
#include <QChartView>
#include <QLineSeries>
#include <QCategoryAxis>
#include <iostream>

using namespace std;
extern QString fileDir;
DrawGraph::DrawGraph(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DrawGraph)
{
    ui->setupUi(this);

    chart = new QtCharts::QChart;
    chartView = new QtCharts::QChartView(chart, this);
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(chartView);
    setLayout(layout);

    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &DrawGraph::draw);
    timer->start(5000);
}

DrawGraph::~DrawGraph()
{

    delete setmrtsConfigUi;
    delete timer;
    delete chart;
    delete chartView;
    delete ui;
}

void DrawGraph::closeEvent(QCloseEvent *event)
{
    timer->stop();

    QWidget::closeEvent(event);
}

void DrawGraph::draw()
{
    chart->removeAllSeries(); // 清除之前的系列数据

    QColor colors[15];
    QColor baseColors[] = {
        QColor(255, 0, 0),    // 红色
        QColor(120, 255, 10),    // 绿色
        QColor(0, 0, 255),    // 蓝色
        QColor(128, 0, 128),   // 紫红色
        QColor(0, 128, 255),  // 淡蓝色
        QColor(255, 0, 255),  // 品红
        QColor(0, 255, 255),  // 青色
        QColor(255, 128, 0),  // 橙色
        QColor(128, 255, 0),  // 淡绿色
        QColor(255, 0, 128),  // 粉红
        QColor(128, 0, 255),  // 紫色
        QColor(0, 255, 128),  // 草绿色
        QColor(128, 128, 0),  // 橄榄色
        QColor(0, 128, 128),  // 蓝绿色
        QColor(255, 255, 0)  // 黄色
    };

    // 初始化15种颜色
    for (int i = 0; i < 15; ++i) {
        colors[i] = baseColors[i % 15]; // 使用预定义的基础颜色
    }

    QString filename = fileDir+"/result.csv";
    qDebug()<<"filename"<<filename<<endl;
    QVector<double> xData;
    QVector<QString> algorithmNames;
    //若干个y轴  其实就是若干个算法
    QVector<QVector<double>> yData;
    QFile file(filename);
    if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream stream(&file);
        // 读取CSV文件的每一行数据
        while (!stream.atEnd()) {
            QString line = stream.readLine();
            if (line.startsWith("NR")) {
                QStringList values = line.split(',');
                for (QString str : values) {
                    algorithmNames.append(str);
                }
                continue;
            }
            //每一行的字符串
            QStringList values = line.split(',');
            qDebug() << values << endl;
            // 跳过空行
            if (values.isEmpty()||values.contains("nan")) {
                continue;
            }
            QVector<double> rowData; //转换成每一行的数据

            for (int i = 0; i < values.size(); i++) {
                bool ok;
                double value = values[i].toDouble(&ok);
                if (!ok) {
                    rowData.append(0);
                    continue;
                }
                rowData.append(value);
            }
            xData.append(rowData[0]); //横轴
            QVector<double> yValues;

            for (int i = 1; i < rowData.size(); i++) {
                yValues.append(rowData[i]); //算法1
            }
            yData.append(yValues);
        }
        file.close();

        // 创建折线系列
        for (int i = 0; i < algorithmNames.size() - 1; i++) {
            QtCharts::QLineSeries *series = new QtCharts::QLineSeries;
           // 设置折线系列的名称和颜色
            series->setName(algorithmNames[i + 1]);

            QPen pen(colors[i]);
            pen.setWidth(i+1);
            series->setPen(pen);
            // 添加数据到折线系列
            for (int j = 0; j < xData.size(); ++j) {
                series->append(xData[j], yData[j][i]); // 第一列纵坐标数据
            }
            // 将折线系列添加到图表中
            chart->addSeries(series);
        }

        // 设置图表的标题和轴标签
        chart->setTitle("Line Chart");
        chart->createDefaultAxes();

        // 创建一个自定义的轴线颜色，以区分不同的折线系列
        QPen pen;
        pen.setColor(QColor(Qt::darkGray));
        chart->axisX()->setLinePen(pen);
        chart->axisY()->setLinePen(pen);

        // 创建图例并设置位置
        chart->legend()->setVisible(true);
        chart->legend()->setAlignment(Qt::AlignBottom);

        // 更新图表显示
        chartView->update();
    }
}

void DrawGraph::on_showTable_clicked()
{
//    resultTable = new ResultTable;
//    resultTable->show();
}

